/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.action.style;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.util.EventObject;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;

import com.reportgear.designer.action.StyleAction;
import com.reportgear.report.core.api.compare.ComparatorUtils;
import com.reportgear.report.model.cell.style.Style;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class ReportFontNameAction extends StyleAction {

    public ReportFontNameAction() {
        this.setName("修改字体");
    }

//    public void applyStyle(Style style) {
//        if (style == null) {
//            return;
//        }
//
//        Object o = getValue(JComboBox.class.getName());
//        if ((o != null) && (o instanceof JComboBox)) {
//            String name = (String) ((JComboBox) o).getSelectedItem();
//            System.out.println("处理选择：" + name);
//        }
//    }

    @Override
    public JComponent createToolBarComponent() {
        Object c = getValue(JComboBox.class.getName());
        if ((c == null) || (!(c instanceof JComboBox))) {
            JComboBox fontComboBox = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
            putValue(JComboBox.class.getName(), fontComboBox);
            fontComboBox.setPreferredSize(new Dimension(Math.min(140, fontComboBox.getPreferredSize().width), fontComboBox.getPreferredSize().height));
            fontComboBox.setEnabled(isEnabled());
            fontComboBox.addActionListener(this);
            return fontComboBox;
        }
        return ((JComboBox) c);
    }

    public void setFontName(String fontName) {
        Object o = getValue(JComboBox.class.getName());
        if ((o == null) || (!(o instanceof JComboBox))) {
            return;
        }
        JComboBox c = (JComboBox) o;
        ComboBoxModel cm = c.getModel();
        for (int i = 0; i < cm.getSize(); ++i) {
            Object name = cm.getElementAt(i);
            if (!(ComparatorUtils.equals(name, fontName))) {
                continue;
            }
            c.removeActionListener(this);
            c.setSelectedIndex(i);
            c.addActionListener(this);

            return;
        }
    }

	public Style getStyle() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public boolean executeAction(EventObject evt) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
